package scuba.DiveBuddy.Fragments;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import org.joda.time.DateTime;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Alexander Anderson on 12/02/14.
 */
public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    Date Time;
    public Date getTime() { return Time; }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it

        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        Calendar c = Calendar.getInstance();
        c.set(0, 0, 0, hourOfDay, minute);
        Time = c.getTime();

        ((DatePickerFragment.DateSetListener)getActivity()).onDateSet(this, new DateTime(getTime()));
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);

        DatePickerFragment.DateSetListener mListener;
        try {
            mListener = (DatePickerFragment.DateSetListener)activity;
        }
        catch(ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement DateSetListener");
        }
    }
}
